package com.yuanmei.views;

import android.app.Activity;
import android.content.ActivityNotFoundException;
import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.Window;
import android.webkit.WebResourceRequest;
import android.webkit.WebResourceResponse;
import android.webkit.WebSettings;
import android.webkit.WebView;
import android.webkit.WebViewClient;

import com.yuanmei.common.Utils;

import java.net.URISyntaxException;

public class EastMoneyViewActivity extends Activity{
    private final static String TAG = EastMoneyViewActivity.class.getName();
    private WebView mWebView;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        String mName = getIntent().getStringExtra("Name");
        String mNetEaseCode = getIntent().getStringExtra("NetEaseCode");
        boolean mIsMarketIndex = getIntent().getBooleanExtra("IsMarketIndex", false);
        requestWindowFeature(Window.FEATURE_NO_TITLE);
        setContentView(R.layout.activity_websource_view);

        mWebView = findViewById(R.id.web_view);

        WebSettings setting = mWebView.getSettings();
        setting.setJavaScriptEnabled(true);

        mWebView.setWebViewClient(new WebViewClient(){
            private String TAG = "WebView";
            @Override
            public boolean shouldOverrideUrlLoading(WebView wv, String url) {
                Log.i(TAG, "shouldOverrideUrlLoading url=" + url);
//                wv.loadUrl(url); //在当前的webview中跳转到新的url
                if (url.startsWith("http") || url.startsWith("https") || url.startsWith("ftp")) {
                    //不处理http, https, ftp的请求
                    return false;
                }
                Intent intent = null;
                try {
                    intent = Intent.parseUri(url, Intent.URI_INTENT_SCHEME);
                    if (startActivityIfNeeded(intent, -1)) {
                        Log.i(TAG, "success start intent url=" + url);
                        return true;
                    }
                } catch (URISyntaxException | ActivityNotFoundException | SecurityException e) {
                    Log.e(TAG, "fail to start intent url:" + url, e);
                    return false;
                }
                return false;
            }
//
//            @Override
//            public WebResourceResponse shouldInterceptRequest(WebView view,
//                                                              WebResourceRequest request){
//                Log.i(TAG, "shouldInterceptRequest url=" +
//                        request.getUrl().toString());
//                return null;
//            }

//            @Override
//            public void onLoadResource(WebView view, String url) {
//                Log.i(TAG, "onLoadResource url=" + url);
//            }
        });

        String emCode = Utils.toEastMoneyCode(mNetEaseCode, mIsMarketIndex);
        String url = "https://emwap.eastmoney.com/quota/stock/index/" + emCode;
        mWebView.loadUrl(url);
    }
    @Override
    public void onBackPressed() {
        if (mWebView.canGoBack()) {
            mWebView.goBack();
        } else {
            super.onBackPressed();
        }
    }
    @Override
    protected void onNewIntent(Intent intent) {
        super.onNewIntent(intent);
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
    }
}
